package offer;
//整数中1出现的次数（从1到n整数中1出现的次数）
public class T32 {
    public int NumberOf1Between1AndN_Solution(int n) {
        int ans = 0;
        for (int i = 0; i < n; i++) {
            int k = i;
            while (k>0) {
                if (k%10==1) ans++;
                k/=10;
            }
        }
        return ans;
    }
    public int NumberOf1Between1AndN_Solution2(int n) {
        int ans = 0;
        for (int i = 1; i <= n; i*=10) {
            int m =n%i;
            int k = n/i;
            ans += (k+8)/10*i+(k%10==1?1:0)*(m+1);
        }
        return ans;
    }
}
